package com.zhb.oxm;

import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Date;
import java.util.List;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;

import com.google.common.collect.Lists;
import com.zhb.entity.Address;
import com.zhb.entity.Card;
import com.zhb.entity.People;

public class TestMarshaller {
	
	private static People initData(){
		People people = new People();
		people.setId(10086);
		people.setAge(25);
		people.setBirthday(new Date());
		people.setFirstName("龚");
		people.setLastName("训瑶");
		
		Address address = new Address();
		address.setProvince("安徽");
		address.setCity("广德");
		address.setCode(242218);
		address.setDetail("杨滩乡胡村村海口冲90号");
		
		List<Card> cards = Lists.newArrayList();
		cards.add(new Card("6227 0013 7655 0927 777","建设银行"));
		cards.add(new Card("6227 0013 7655 0927 888","招商银行"));
		cards.add(new Card("6227 0013 7655 0927 999","中国银行"));
		people.setAddress(address);
		people.setCards(cards);
		return people;
	}
	
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Jaxb2Marshaller marshaller = context.getBean("jaxb2Marshaller", Jaxb2Marshaller.class);
		People people = initData();
		
		Writer writer = new StringWriter();
		StreamResult result = new StreamResult(writer);
		marshaller.marshal(people, result);
		System.out.println(writer.toString());
		
		Reader reader = new StringReader(writer.toString());
		
		People people2 = (People) marshaller.unmarshal(new StreamSource(reader));
		
		System.out.println(people2);
	}

}
